﻿using System;
using System.Diagnostics;
using System.IO;

namespace SXICE.Core.Infrastructure
{
    /// <summary>
    /// 默认日志
    /// </summary>
    public class DefaultLog : ILog
    {
        /// <summary>
        /// 文件路径
        /// </summary>
        private readonly string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt");
        /// <summary>
        /// 锁对象
        /// </summary>
        private static readonly object lockObj = new object();

        public void Debug<T>(T content)
        {
            Write(content, "Debug");
        }

        public void Error(Exception ex)
        {
            Write(ex.ToString(), "Error");
        }

        public void Info<T>(T content)
        {
            Write(content, "Info");
        }

        public void Warn<T>(T content)
        {
            Write(content, "Warn");
        }

        private void Write<T>(T content, string level)
        {
            var stack = new StackTrace();
            var method = stack.GetFrame(2).GetMethod();
            lock (lockObj)
                File.AppendAllText(filePath, string.Format($"{level}-{method.DeclaringType.FullName}.{method.Name}()-{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}：{content}{Environment.NewLine}{Environment.NewLine}"));
        }
    }
}
